package com.java.comprehensive.arithmetic.leecode.medium;

import org.apache.poi.util.ArrayUtil;

import java.util.*;

/**
 * @Author: 窦绍飞
 * @Date: 2022/7/31
 */
public class LeetCode_17 {
	Map<Character, String> map = new HashMap<>();

	public List<String> letterCombinations(String digits) {
		List<String> res = new ArrayList<>();
		if (digits == null || digits.isEmpty()) return res;
		map.put('2', "abc");
		map.put('3', "def");
		map.put('4', "ghi");
		map.put('5', "jkl");
		map.put('6', "mno");
		map.put('7', "pqrs");
		map.put('8', "tuv");
		map.put('9', "wxyz");
		helper(res, digits, 0, new StringBuffer());
		return res;
	}

	/**
	 * 回溯 进入方法找到出口 出口就是 纪录单个对应最长的字符串就是一个唯一的值纪录完毕就结束了，
	 *
	 * @param res         纪录每个结果集
	 * @param digits      字符串
	 * @param start       以谁为基准 纪录数据的索引
	 * @param combination 组合每个值的结果
	 */

	private void helper(List<String> res, String digits, int start, StringBuffer combination) {
		if (start == digits.length()) {
			res.add(combination.toString());
		} else {
			char c = digits.charAt(start);
			String phoneNumber = map.get(c);
			int length = phoneNumber.length();
			for (int i = 0; i < length; i++) {
				combination.append(phoneNumber.charAt(i));
				helper(res, digits, start + 1, combination);
				// 清空上次纪录的最后一个字母比如Ad 纪录过后改纪录 ae了，将d删除
				combination.deleteCharAt(start);
			}
		}
	}

	public static void main(String[] args) {
		LeetCode_17 leetCode_17 = new LeetCode_17();
		List<String> strings = leetCode_17.letterCombinations("23");
		System.out.println(Arrays.toString(new List[]{strings}));
	}
}
